﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using XZTZ.UMP.Public.Common;
using XZTZ.UMP.Public.Models.Base;
using XZTZ.UMP.WEB.Data;

namespace XZTZ.UMP.WEB.Pages.Base.BillProperty
{
    public class IndexModel : PageModel
    {
        private readonly XZTZ.UMP.WEB.Data.ApplicationDbContext _context;

        public IndexModel(XZTZ.UMP.WEB.Data.ApplicationDbContext context)
        {
            _context = context;
        }
		//public IList<XZTZ.UMP.Public.Models.Base.BillProperty> BillProperty { get;set; }
		//public async Task OnGetAsync()
		//{
		//    BillProperty = await _context.Base_BillPropertys.ToListAsync();
		//}


		public string StrPropertyName { get; set; }
		public string DateSort { get; set; }
		public string CurrentFilter { get; set; }
		public string CurrentSort { get; set; }

		public PaginatedList<XZTZ.UMP.Public.Models.Base.BillProperty> BillProperty { get; set; }

		public async Task OnGetAsync(string sortOrder,	string currentFilter, string searchString, int? pageIndex)
		{
			CurrentSort = sortOrder;
			StrPropertyName = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
			DateSort = sortOrder == "Date" ? "date_desc" : "Date";

			if (searchString != null)
			{
				pageIndex = 1;
			}
			else
			{
				searchString = currentFilter;
			}
			CurrentFilter = searchString;
			IQueryable<XZTZ.UMP.Public.Models.Base.BillProperty> billPropertyIQ = from s in _context.Base_BillPropertys
											select s;
			if (!String.IsNullOrEmpty(searchString))
			{
				billPropertyIQ = billPropertyIQ.Where(s => s.StrPropertyName.ToUpper().Contains(searchString.ToUpper()));
			}
			switch (sortOrder)
			{
				case "name_desc":
					billPropertyIQ = billPropertyIQ.OrderByDescending(s => s.StrPropertyName);
					break;
				case "Date":
					billPropertyIQ = billPropertyIQ.OrderBy(s => s.DtmOperateDate);
					break;
				case "date_desc":
					billPropertyIQ = billPropertyIQ.OrderByDescending(s => s.DtmOperateDate);
					break;
				default:
					billPropertyIQ = billPropertyIQ.OrderBy(s => s.StrPropertyName);
					break;
			}

			//BillProperty = await billPropertyIQ.AsNoTracking().ToListAsync();
			int pageSize = 3;
			BillProperty = await PaginatedList <XZTZ.UMP.Public.Models.Base.BillProperty >.CreateAsync(
				billPropertyIQ.AsNoTracking(), pageIndex ?? 1, pageSize);
		}
	}
}
